package com.yuncheng.spcyApi.service.impl;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SysUserFgld;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.mapper.SysUserFgldMapper;
import com.yuncheng.spcyApi.service.ISysUserFgldService;
import com.yuncheng.spcyApi.service.IYcSysOrgService;
import com.yuncheng.spcyApi.service.IYcSysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 系统-分管领导配置
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SysUserFgldServiceImpl extends BaseServiceImpl<SysUserFgldMapper, SysUserFgld> implements ISysUserFgldService {

    @Resource
    private IYcSysOrgService ycSysOrgService;

    @Resource
    private IYcSysUserService ycSysUserService;

    @Override
    public YcSysUser getFgldUserBydept(String deptName) {
        if (StringUtils.isEmpty(deptName)){
            return null;
        }
        deptName = SpcyConstant.getYwclNameByYWMC(deptName);
        // 获取部门
        List<YcSysOrg> deptList = ycSysOrgService.list(new LambdaQueryWrapper<YcSysOrg>()
                .like(YcSysOrg::getOrgName, deptName));
        if (CollectionUtils.isNotEmpty(deptList)){
            YcSysOrg ycSysOrg = deptList.get(0);
            String deptId = ycSysOrg.getId();

            // 获取部门的分管领导用户
            List<SysUserFgld> fgldList = list(new LambdaQueryWrapper<SysUserFgld>()
                    .eq(SysUserFgld::getFdeptid, deptId));
            if (CollectionUtils.isNotEmpty(fgldList)){
                SysUserFgld sysUserFgld = fgldList.get(0);
                String fuserId = sysUserFgld.getFuserid();

                // 获取用户
                YcSysUser sysUser = ycSysUserService.getById(fuserId);
                return sysUser;
            }
        }

        return null;
    }

    @Override
    public YcSysUser getFgldUserBydeptId(String deptId) {
        // 获取部门的分管领导用户
        List<SysUserFgld> fgldList = list(new LambdaQueryWrapper<SysUserFgld>()
                .eq(SysUserFgld::getFdeptid, deptId));
        if (CollectionUtils.isNotEmpty(fgldList)){
            SysUserFgld sysUserFgld = fgldList.get(0);
            String fuserId = sysUserFgld.getFuserid();

            // 获取用户
            YcSysUser sysUser = ycSysUserService.getById(fuserId);
            return sysUser;
        }
        return null;
    }
}
